package com.cjw.controller.basedata;

import com.cjw.config.annotation.OperationLogAnnotation;
import com.cjw.pojo.dto.database.PageDTO;
import com.cjw.pojo.dto.database.ResourceDTO;
import com.cjw.pojo.dto.database.UserDTO;
import com.cjw.pojo.entity.database.Resource;
import com.cjw.pojo.query.database.AddResourceDTO;
import com.cjw.pojo.query.database.ResourceQuery;
import com.cjw.pojo.query.database.UpdateResourceDTO;
import com.cjw.service.database.ResourceService;
import com.cjw.util.BaseContextHolder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api("用户管理")
public class ResourceController {
    @Autowired
    private ResourceService resourceService;

    @GetMapping("/resource")
    @ApiOperation("查询资源列表")
    public List<ResourceDTO> commonQuery() {
        UserDTO user = (UserDTO) BaseContextHolder.get("user");
        return resourceService.queryByUserId(user.getId());
    }

    @PostMapping("/admin/resource/getList")
    @ApiOperation("分页查询资源列表")
    @OperationLogAnnotation(operModul = "资源管理-菜单权限管理",operType = "分页查询菜单",operDesc = "查询")
    public PageDTO<List<Resource>> getList(@RequestBody ResourceQuery resourceQuery) {
        return resourceService.getList(resourceQuery);
    }

    @PostMapping("/admin/resource/add")
    @ApiOperation("添加菜单")
    @OperationLogAnnotation(operModul = "资源管理-菜单权限管理",operType = "添加菜单",operDesc = "添加")
    public Boolean add(@RequestBody AddResourceDTO addResourceDTO) {
       return resourceService.add(addResourceDTO);
    }

    @GetMapping("/admin/resource/detail")
    @ApiOperation("菜单详细")
    @OperationLogAnnotation(operModul = "资源管理-菜单权限管理",operType = "菜单详细",operDesc = "查询")
    public UpdateResourceDTO detail(Long id) {
        return resourceService.detail(id);
    }

    @PostMapping("/admin/resource/update")
    @ApiOperation("修改菜单")
    @OperationLogAnnotation(operModul = "资源管理-菜单权限管理",operType = "修改菜单",operDesc = "修改")
    public Boolean update(@RequestBody UpdateResourceDTO updateResourceDTO) {
        return resourceService.update(updateResourceDTO);
    }

    @PostMapping("/admin/resource/delete")
    @ApiOperation("删除菜单")
    @OperationLogAnnotation(operModul = "资源管理-菜单权限管理",operType = "删除菜单",operDesc = "修改")
    public Boolean delete(Long id) {
        return resourceService.delete(id);
    }

    @PostMapping("/admin/resource/changeState")
    @ApiOperation("修改菜单状态")
    @OperationLogAnnotation(operModul = "资源管理-菜单权限管理",operType = "修改菜单状态",operDesc = "修改")
    public Boolean changeState(Long id) {
        return resourceService.changeState(id);
    }
}
